<!-- 修改新闻类型 -->
<template>
  <cfDialog :dialogObj='dialogObj'>
    <div slot='content' v-loading="loading">
      <el-form ref='form' :model='form' :rules="rules" label-width='80px' size='small' label-position='right'>
        <el-form-item label='类型名称' prop="typeName">
          <el-input v-model='form.typeName' placeholder='请输入类型名称' />
        </el-form-item>
        <el-form-item label='是否公有' prop="publicFlag">
          <el-select v-model='form.publicFlag' placeholder='请选择类型性质' :disabled="!!dialogObj.id" style="width: 100%;">
            <el-option v-for="dict in sysYesNo" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label='排序' prop="orderNum">
          <el-input type="number" :min="0" :max="100" v-model='form.orderNum' placeholder='请输入排序' />
        </el-form-item>
        <el-form-item label='备注' prop="typeDesc">
          <el-input type="textarea" :rows="5" v-model='form.typeDesc' placeholder='请输入备注' />
        </el-form-item>
      </el-form>
    </div>
    <template slot='footer'>
      <el-button type='primary' size='small' v-if="!loading" @click='handleConfirm'>确定</el-button>
    </template>
  </cfDialog>
</template>
<script>
import news from '@/api/news'
export default {
  props: {
    dialogObj: {
      type: Object,
      default: ({
        visible: false,
        type: '',
        id: '',
        title: '',
        width: '520px'
      })
    }
  },
  data() {
    return {
      form: {},
      rules: {
        typeName: [{required: true, message: '类型不得为空', trigger: 'change'}],
        publicFlag: [{required: true, message: '请选择类型性质', trigger: 'change'}],
        orderNum: [{required: true, message: '排序不得为空', trigger: 'change'}],
      },
      loading: false
    }
  },
  created() {
    this.getSysYesNo();
    if(this.dialogObj.id) {
      this.getDetailFn();
    } else {
      this.form = {
        typeId: '',
        typeName: '',
        publicFlag: '',
        typeDesc: '',
        orderNum: ''
      };
    }
  },
  methods: {
    /**确定按钮提交功能*/
    handleConfirm() {
      this.submitForm('form', async ()=>{
        this.loading = true;
        const data = {
          typeName: this.form.typeName,
          typeDesc: this.form.typeDesc,
          orderNum: this.form.orderNum,
          publicFlag: this.form.publicFlag,
          typeId: this.form.typeId
        };
        let res;
        if(!this.dialogObj.id) {
          res = await news.addNoticesType(data);
        } else {
          res = await news.updateNoticesType(data);
        }
        if(res.code == 200) {
          this.$message.success('操作成功');
          this.dialogObj.visible = false;
          this.dialogObj.component = '';
          this.$emit('refreshTree');
        }
        setTimeout(() => {
          this.loading = false;
        }, 500);
      })
    },
    /**获取弹窗详情*/
    async getDetailFn() {
      this.loading = true;
      const data = {
        typeId: this.dialogObj.id
      }
      const res = await news.noticesTypeDetail(data);
      if(res.code == 200) {
        this.form = res.data;
      }
      setTimeout(() => {
        this.loading = false;
      }, 500);
    }
  }
}
</script>